package com.hejx.util;

import lombok.extern.slf4j.Slf4j;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * Created by 追风少年
 *
 * @email doubihah@foxmail.com
 * @create 2018/1/5 11:04
 **/
@Slf4j
public class ExceptionUtil {

    /**
     * 获取异常的堆栈信息
     * @param e
     * @return
     */
    public static String getErrorInfoByException(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            e.printStackTrace(pw);
            sw.close();
            pw.close();
            String result = sw.toString();
            return "\r\n" + result + "\r\n";
        } catch (Exception e2) {
            log.error("getErrorInfoFromException failed e2:"+e2);
            return "获取异常堆栈信息失败";
        }
    }

}
